// 导入calendar模块中的calendar对象
// const cal = require('calendar').Calendar
// const {Solar, Lunar,HolidayUtil} = require('lunar-javascript')
// 导入自定义模块(datetime)
const datetime = require('../../moduls/datetime')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    year: 0,  //用户选择的年(或当前系统的年份)
    month: 0,  //用户选择的月(或当前系统的月份)
   today: {},
    currentYearMonth: '2025-06',
    now: '2022-04-18 17:38:20',
    // 1.多行一列使用array
    // 2.一行多列使用object
   // 3. 多行多列使用对象数组(array/object)
  calendarDatas: [
    [
      {year: 2022, month: 3,day: 27,week: "日", lunarDay: "廿五",isWork: false,holiday: ""},
      {year: 2022, month: 3,day: 28,week: "一", lunarDay: "廿六",isWork: false,holiday: ""},
      {year: 2022, month: 3,day: 29,week: "二", lunarDay: "廿七",isWork: false,holiday: ""},
      {year: 2022, month: 3,day: 30,week: "三", lunarDay: "廿八",isWork: false,holiday: ""},
      {year: 2022, month: 3,day: 31,week: "四", lunarDay: "廿九",isWork: false,holiday: ""},
      {year: 2022, month: 4,day: 1,week: "五", lunarDay: "三月",isWork: false,holiday: ""},
      {year: 2022, month: 4,day: 2,week: "六", lunarDay: "初二",isWork: false,holiday: ""}
    ],
    [
        {year: 2022, month: 4,day: 3,week: "日", lunarDay: "初三",isWork: false,holiday: ""},
      {year: 2022, month: 4,day: 4,week: "一", lunarDay: "初四",isWork: false,holiday: ""},
      {year: 2022, month: 4,day: 5,week: "二", lunarDay: "初五",isWork: false,holiday: ""},
      {year: 2022, month: 4,day: 6,week: "三", lunarDay: "初六",isWork: false,holiday: ""},
      {year: 2022, month: 4,day: 7,week: "四", lunarDay: "初七",isWork: false,holiday: ""},
      {year: 2022, month: 4,day: 8,week: "五", lunarDay: "初八",isWork: false,holiday: ""},
      {year: 2022, month: 4,day: 9,week: "六", lunarDay: "初九",isWork: false,holiday: ""}
    ]
  ]
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTime()
    let todayStr = datetime.format.date(new Date())
    let tmp = todayStr.split('-')
    let today = {
      year: tmp[0] * 1,
      month: tmp[1] * 1,
      day: tmp[2] * 1
    }
    this.setData({
      year: tmp[0] * 1,
      month: tmp[1] * 1,
      currentYearMonth: datetime.format.yearMonth(new Date()),
      today,
      calendarDatas: datetime.calendarInLunar(today.year, today.month, true)
    })
  },
  // 获取当前时间
  getTime() {
    // 计时器setInterval()与setTimeout()
    // setInterval():间隔指定的时间，调用函数(周期)
    // setTimeout():延时指定的时间，调用函数(一次)
    setInterval(()=>{
      let now = new Date()
      this.setData({
        now: datetime.format.date(now) + ' ' + datetime.format.time(now)
      })
    },1000)
  },
    // JS中的NaN:NotaNumber(不是一个数字)
  onPicker(e) {
    let value = e.detail.value  //'2022-04'
    let tmp = value.split('-')
    // let day = this.data.today.day //获取data中的变量
    this.setData({
      year: tmp[0] * 1,
      month: tmp[1] * 1,
      currentYearMonth: value,
      calendarDatas: datetime.calendarInLunar(tmp[0] * 1, tmp[1] * 1, true)
    })
  },
  // 短触“日”的事件处理程序
  onDay(event) {
    let {year, month, day} = event.currentTarget.dataset
    wx.navigateTo({
      url: `../detail/index?y=${year}&m=${month}&d=${day}`,
    })
  },
  // 短触“+”的事件处理程序
  onPlus(event) {
    wx.navigateTo({
      url: '../add/index',
    })
  }
})
/** 
 * 传递敬据：
 * JS -> WXML：通过页面变量(data)
 * WXML->JS：组件的自定义属性（data-xxx)
*/